<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page import="com.lukebang.util.RSAUtils"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.security.interfaces.*"%>
<c:set var="ctxPath" value="${pageContext.request.contextPath}"
	scope="application" />
<!DOCTYPE html>
<html lang="en">
<head>
<title>登录鹿客帮</title>
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta name="description" content="">
<meta name="author" content="ThemeBucket">
<script src="${ctxPath}/static/js/jquery-1.10.2.min.js"></script>
<link rel="shortcut icon" type="image/x-icon"
	href="static/images/lukebang320.png" media="screen" />
<link rel="icon" href="static/images/lukebang320.ico"
	type="image/x-icon" />
<link rel="bookmark" href="static/images/lukebang320.ico"
	type="image/x-icon" />

<link rel="stylesheet" type="text/css" href="static/css/style.css">
<link rel="stylesheet" type="text/css"
	href="static/css/style-responsive.css">
<link rel="stylesheet" type="text/css"
	href="static/js/useful-toastr/toastr.min.css" />
<link href="${ctxPath}/static/css/jquery.stepy.css" rel="stylesheet">
<style type="text/css">
.fontSize {
	font-size: 12px;
}
</style>
<%
	HashMap<String, Object> map = RSAUtils.getKeys();
	//生成公钥和私钥    
	RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
	RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
	session.setAttribute("privateKey", privateKey);//私钥保存在session中，用于解密  
	//公钥信息保存在页面，用于加密  
	String publicKeyExponent = publicKey.getPublicExponent().toString(16); //公钥指数   
	String publicKeyModulus = publicKey.getModulus().toString(16); //模    
	request.setAttribute("publicKeyExponent", publicKeyExponent);
	request.setAttribute("publicKeyModulus", publicKeyModulus);
%>
</head>

<body class="login-body">
	<div class="container">
		<form id="form" class="form-signin" method="post" name="FormEnter">
			<div class="form-signin-heading text-center">
				<img src="static/images/login-logo3.png" width="118" height="113"
					alt="" />
			</div>
			<div class="login-wrap">
				<input type="text" class="form-control" name="phone" id="tx_phone"
					onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="手机号"
					autofocus value="" onkeydown="KeyDown()"> <input
					type="password" style="display: none" /> <input type="password"
					class="form-control" name="password" id="tx_password"
					onkeyup="this.value=this.value.replace(/^ +| +$/g,'')"
					placeholder="密码" value="" onkeydown="KeyDown()"> <input
					class="btn btn-lg btn-login btn-block" type="button"
					onclick="login()" value="登录" name="btnSubmit">
				<div align="right" style="margin-top: 10px">
					<input class="btn  btn-sm" type="button" data-toggle="modal"
						data-target="#zhuceModal"
						onclick="javascript:$('#registerForm')[0].reset()" value="注册">
					<label for="remPwd" style="float: left;"><input
						type="checkbox" id="remPwd" class="inpBtn"
						style="margin: 7px 1px 0;" /><label for="remPwd"
						style="font-size: 8px; color: #7f7f84; font-weight: 400; padding-left: 0; padding-right: 0;vertical-align: middle;">记住密码</label></label>
					<input class="btn btn-sm" type="button"
						style="background: #fff; float: left;padding: 2px 10px;" data-toggle="modal"
						data-target="#retrievalModal" value="忘记密码?">
				</div>
			</div>
		</form>
	</div>
	<p align="center">
		备案号：<a href="http://www.miibeian.gov.cn/" target="_blank">豫ICP备17004523号</a>
	</p>
	<!-- 查看详情的模态框 1-->
	<div class="modal fade" id="zhuceModal" tabindex="-1" role="dialog"
		data-backdrop='static' data-keyboard='false'
		aria-labelledby="myLargeModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">&times;</span> <span class="sr-only">Close</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">注册</h4>
				</div>
				<div class="modal-body">
					<!--这是模态框内容  -->
					<form method="post" class="form-horizontal" id="registerForm"
						style="position: relative; margin-left: 111px;">
						<div class="form-group" id="registerTypeDiv">
							<label class="col-md-3 control-label">类型：</label>
							<div class="col-md-6">
								<div class="radio-list">
									<label class="radio-inline"> <input type="radio"
										onclick="changeRegisterType(this)" name="type" value="1"
										checked> 个人
									</label> <label class="radio-inline"> <input type="radio"
										onclick="changeRegisterType(this)" name="type" value="0">
										商家
									</label>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">姓名：</label>
							<div class="col-md-6">
								<input type="text" placeholder="姓名" class="form-control"
									onkeyup="this.value=this.value.replace(/^ +| +$/g,'')"
									id="input_name" required name="name">
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 control-label">性别：</label>
							<div class="col-md-6">
								<div class="radio-list">
									<label class="radio-inline"> <input type="radio"
										name="sex" value="1" checked> 男
									</label> <label class="radio-inline"> <input type="radio"
										name="sex" value="0"> 女
									</label>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">手机号：</label>
							<div class="col-md-6">
								<input type="text" placeholder="手机号" required
									onkeyup="this.value=this.value.replace(/\D/g,'')"
									maxlength="11" class="form-control" id="register_phone"
									name="phone">
							</div>
							<div class="col-md-3">
								<button type="button" class="btn btn-primary btn-xs"
									onclick="sendIndentifyCode(this)"
									style="display: block; margin-top: 5px; width: 80px">发送验证码</button>
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">验证码：</label>
							<div class="col-md-6">
								<input type="text" required placeholder="验证码" maxlength="6"
									onkeyup="this.value=this.value.replace(/\D/g,'')"
									class="form-control" name="verifyCode">
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">密码：</label>
							<div class="col-md-6">
								<input type="password" placeholder="密码" required
									onkeyup="this.value=this.value.replace(/^ +| +$/g,'')"
									class="form-control" id="password" name="password">
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">确认密码：</label>
							<div class="col-md-6">
								<input type="password" placeholder="密码" required
									onkeyup="this.value=this.value.replace(/^ +| +$/g,'')"
									class="form-control" name="confirm_password">
							</div>
						</div>
					</form>
					<!-- 这是模态框内容结束 -->
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" id="myClose"
						onclick="clearRegisterForm()" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-default"
						onclick="registerMerchant()">注册</button>
				</div>
			</div>
		</div>
	</div>

	<div class="modal fade" id="retrievalModal" tabindex="-1" role="dialog"
		data-backdrop='static' data-keyboard='false'
		aria-labelledby="retrievalModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="retrievalModalLabel">重置密码</h4>
				</div>
				<div class="modal-body">
					<!--这是模态框内容  -->
					<form method="post" class="form-horizontal" id="resetPasswordForm"
						style="position: relative; margin-left: 111px;">
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">手机号：</label>
							<div class="col-md-6">
								<input type="text" placeholder="手机号" required
									onkeyup="this.value=this.value.replace(/\D/g,'')"
									maxlength="11" class="form-control" id="register_phone"
									name="phone">
							</div>
							<div class="col-md-3">
								<button type="button" class="btn btn-primary btn-xs"
									onclick="sendIndentifyCode(this)"
									style="display: block; margin-top: 5px; width: 80px">发送验证码</button>
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">验证码：</label>
							<div class="col-md-6">
								<input type="text" required placeholder="验证码" maxlength="6"
									onkeyup="this.value=this.value.replace(/\D/g,'')"
									class="form-control" name="verifyCode">
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">密码：</label>
							<div class="col-md-6">
								<input type="password" placeholder="密码" required
									onkeyup="this.value=this.value.replace(/^ +| +$/g,'')"
									class="form-control" id="reset_password" name="password">
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-3 col-sm-3 control-label">确认密码：</label>
							<div class="col-md-6">
								<input type="password" placeholder="密码" required
									onkeyup="this.value=this.value.replace(/^ +| +$/g,'')"
									class="form-control" name="confirm_password">
							</div>
						</div>
					</form>
					<!-- 这是模态框内容结束 -->
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default"
						onclick="clearResetPasswordForm()" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-default"
						onclick="resetPassword()">重置</button>
				</div>
			</div>
		</div>
	</div>

</body>
<script src="static/js/jquery-1.10.2.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/js/modernizr.min.js"></script>
<script src="${ctxPath}/static/js/security/security.js"></script>
<!-- toastr提示框的使用 -->
<script src="static/js/useful-toastr/toastr.min.js"></script>
<script src="static/js/useful-ewin/Ewin.js"></script>
<script src="static/js/jquery.validate.min.js"></script>
<script src="static/js/html5shiv.js"></script>
<script src="static/js/respond.min.js"></script>
<script type="text/javascript">
	function makeInput(labContent, placeholder, name) {
		var div = $('<div class="form-group"></div>');
		var label = $('<label class="col-md-3 col-sm-3 control-label">'
				+ labContent + '：</label>');
		var input = ('<div class="col-md-6"><input type="text" required placeholder="' + placeholder
				+ '" class="form-control" name="' + name + '"/></div>');
		div.append(label);
		div.append(input);
		return div;
	}
	function sendIndentifyCode(obj) {
		var phone = $('#register_phone', $(obj).closest('form')).val();
		var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
		if (!phone || phone.length != 11 || !(mobile.test(phone))) {
			return toastr.info("手机号不合法");
		}
		//如果是注册
		if ($(obj).closest('form').attr('id') == 'registerForm') {
			$.get("${ctxPath }/merchant/isPhoneAlreadyUsed", {
				phone : phone
			}, function(data) {
				if (!data) {
					$.get("${ctxPath }/merchant/sendVerifyCode", {
						phone : phone
					}, function(data) {
						if (!data) {
							toastr.info("验证码发送失败");
						}
					});
					clock = setInterval(function() {
						settime(obj)
					}, 1000)
				} else {
					toastr.info("手机号已经注册，请更换其他手机号进行注册。");
				}
			});
		} else {
			$.get("${ctxPath }/merchant/isPhoneAlreadyUsed", {
				phone : phone
			}, function(data) {
				if (!data) {
					toastr.info("手机号并未注册，无法进行下一步操作！");
				} else {
					$.get("${ctxPath }/merchant/sendVerifyCode", {
						phone : phone
					}, function(data) {
						if (!data) {
							toastr.info("验证码发送失败");
						}
					});
					clock = setInterval(function() {
						settime(obj)
					}, 1000)
				}
			});
		}
	}
	var countdown = 60;
	function settime(obj) {
		if (countdown == 0) {
			obj.removeAttribute("disabled");
			$(obj).html("发送验证码");
			countdown = 60;
			clearInterval(clock);
			return;
		} else {
			obj.setAttribute("disabled", true);
			$(obj).html(countdown + "秒后重发");
			countdown--;
		}
	}
	function changeRegisterType(object) {
		if ($(object).val() == 0) {
			$("#registerForm").find("input[name='companyName']").parent()
					.parent().remove();
			$('#registerTypeDiv').after(
					makeInput('商家名称', '商家名称', 'companyName'));
			$("#registerForm input").not("input[name='type']").val("");
		}
		if ($(object).val() == 1) {
			$("#registerForm").find("input[name='companyName']").parent()
					.parent().remove();
			$("#registerForm input").not("input[name='type']").val("");

		}
	}
	//登录
	function login() {
		var phone = $("#tx_phone").val();
		var password = $("#tx_password").val();
		password = password.split("").reverse().join("");
		RSAUtils.setMaxDigits(256);
		var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "",
				"${publicKeyModulus}");//公钥
		var encrypedPwd = RSAUtils.encryptedString(key, password);
		//alert(encrypedPwd);
		$.post("merchant/login", {
			phone : phone,
			password : encrypedPwd
		}, function(data) {
			if (data) {
				//alert("进入登录方法啦啦啦啦！");
				window.location.href = "index.html";
			} else {
				toastr.error("您的账户名和密码有误，请重新输入。");
			}
		});
		//记住密码
		if ($(".inpBtn").get(0).checked) {
			//alert(222);
			window.localStorage.setItem("phone", phone);
			window.localStorage.setItem("password", password);
		} else {
			phone = window.localStorage.removeItem("phone");
			password = window.localStorage.removeItem("password");
		}

	}
	//记住密码赋值
	$(function() {
		var pwd = window.localStorage.getItem("password");
		var phone = window.localStorage.getItem("phone");
		//pwd 转序
		var reverse = function(pwd) {
			//判空 split
			if (pwd == null || pwd == '') {
				return null;
			} else {
				return pwd.split('').reverse().join('');
			}
		};
		$("#tx_phone").val(phone);
		$("#tx_password").val(reverse(pwd));
		//console.log("1---"+pwd);
		//console.log("2---"+reverse(pwd));
	})
	$(function() {
		//初始化toastr
		toastr.options = {
			"closeButton" : false,
			"debug" : false,
			"positionClass" : "toast-top-full-width",
			"showDuration" : "300",
			"hideDuration" : "1000",
			"timeOut" : "5000",
			"extendedTimeOut" : "1000",
			"showEasing" : "swing",
			"hideEasing" : "linear",
			"showMethod" : "fadeIn",
			"hideMethod" : "fadeOut"
		};
		// 手机号码验证  
		jQuery.validator
				.addMethod(
						"isMobile",
						function(value, element) {
							var length = value.length;
							var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
							return this.optional(element)
									|| (length == 11 && mobile.test(value));
						}, "请正确填写您的手机号码");
		$("#registerForm").validate({
			rules : {
				name : {
					required : true,
					minlength : 2,
				},
				verifyCode : {
					required : true,
				},
				phone : {
					required : true,
					minlength : 2,
					isMobile : true
				},
				password : {
					required : true,
					minlength : 6
				},
				confirm_password : {
					required : true,
					minlength : 6,
					equalTo : "#password"
				},
			},
			errorClass : "fontSize error",
			messages : {
				name : {
					required : "请输入用户姓名",
					minlength : "用户姓名长度至少为2"
				},
				verifyCode : {
					required : "请获取验证码后输入",
				},
				phone : {
					required : "请输入手机号",
					minlength : "用户手机号长度至少为2",
					isMobile : "请正确填写您的手机号码"
				},
				password : {
					required : "请输入密码",
					minlength : "密码长度至少为6位"
				},
				confirm_password : {
					required : "请再次输入密码",
					minlength : "密码长度至少为6位",
					equalTo : "两次输入的密码不一致"
				}
			}
		});
		$("#resetPasswordForm").validate({
			rules : {
				verifyCode : {
					required : true,
				},
				phone : {
					required : true,
					minlength : 2,
					isMobile : true
				},
				password : {
					required : true,
					minlength : 6
				},
				confirm_password : {
					required : true,
					minlength : 6,
					equalTo : "#reset_password"
				},
			},
			errorClass : "fontSize error",
			messages : {
				verifyCode : {
					required : "请获取验证码后输入",
				},
				phone : {
					required : "请输入手机号",
					minlength : "用户手机号长度至少为2",
					isMobile : "请正确填写您的手机号码"
				},
				password : {
					required : "请输入密码",
					minlength : "密码长度至少为6位"
				},
				confirm_password : {
					required : "请再次输入密码",
					minlength : "密码长度至少为6位",
					equalTo : "两次输入的密码不一致"
				}
			}
		});
	});
	//重置事件  
	function clearRegisterForm() {
		countdown = 0;//点击关闭,计时器归0
		$("#registerForm").find("input[name='companyName']").parent().parent()
				.remove();
		var validator = $("#registerForm").validate({
			submitHandler : function(form) {
				form.submit();
			}
		});
		validator.resetForm();
	}
	//重置事件  
	function clearResetPasswordForm() {
		countdown = 0;//点击关闭,计时器归0
		var validator = $("#resetPasswordForm").validate({
			submitHandler : function(form) {
				form.submit();
			}
		});
		validator.resetForm();
		$("#resetPasswordForm")[0].reset();
	}
	function registerMerchant() {
		if ($("#registerForm").valid()) {
			var password = $("#registerForm #password").val();
			password = password.split("").reverse().join("");
			RSAUtils.setMaxDigits(256);
			var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "",
					"${publicKeyModulus}");//公钥
			var encrypedPwd = RSAUtils.encryptedString(key, password);
			//提交时，进行加密
			$("#registerForm input[name='password']").first().val(encrypedPwd);
			$("#registerForm input[name='confirm_password']").first().val(
					encrypedPwd);
			$.post("${ctxPath }/merchant/register.htm", $("#registerForm")
					.serialize(),
					function(data) {
						if (data == '00') {
							toastr.success("恭喜您注册成功，您可以登录下单了。");
							$("#myClose").click();
						} else if (data == '22') {
							toastr.info("该手机号码已经注册,请修改手机号码！！");
						} else {
							toastr.info("验证码不正确，请您获取正确的验证码！！");
						}
						$("#registerForm input[name='password']").first().val(
								password);
						$("#registerForm input[name='confirm_password']")
								.first().val(password);
					});
		}
		countdown = 0;
	}
	function resetPassword() {
		if ($("#resetPasswordForm").valid()) {
			var password = $("#resetPasswordForm #reset_password").val();
			password = password.split("").reverse().join("");
			RSAUtils.setMaxDigits(256);
			var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "",
					"${publicKeyModulus}");//公钥
			var encrypedPwd = RSAUtils.encryptedString(key, password);
			//提交时，进行加密
			$("#resetPasswordForm input[name='password']").first().val(
					encrypedPwd);
			$("#resetPasswordForm input[name='confirm_password']").first().val(
					encrypedPwd);
			$.post("${ctxPath }/merchant/reset", $("#resetPasswordForm")
					.serialize(), function(data) {
				if (data) {
					toastr.info("修改密码成功！");
				} else {
					toastr.info("系统错误，请稍后再试！");
				}
				clearResetPasswordForm();
			});
		}
		countdown = 0;
	}

	//按回车键进行登录。
	function KeyDown() {
		if (event.keyCode == 13) {
			event.returnValue = false;
			event.cancel = true;
			FormEnter.btnSubmit.click();
		}
	}
</script>
</html>
